import time
import json
from datetime import datetime
from multiprocessing.pool import ThreadPool

import redis
import requests
from lxml import etree
from utils.json_func import to_json_info

# 检测时间
DETECTIONCOOKIE_TIME = 1 * 24 * 60 * 60

# 是否开启检测
START_DETECTIONCOOKIE = True


class Pools(object):
    """
    Redis Pool
    """

    def __init__(self, host='127.0.0.1', port=6379):
        self.connect = redis.Redis(host=host, port=port, db=0)

    def add_or_set_hash(self, key, mapping):
        try:
            self.connect.hmset(key, mapping=mapping)
            return to_json_info(errmsg='添加成功')
        except Exception as e:
            print('添加失败')
            return to_json_info(errno=1, errmsg='添加失败')

    def get_all_keys(self):
        try:
            keys = self.connect.keys()
            return keys
        except:
            print('获取失败')
            return to_json_info(errno=1)

    def get_one_keys(self, key, field):
        try:
            data = self.connect.hget(key, field)
            return data
        except:
            print('获取失败')
            return to_json_info(errmsg='获取失败')

    def del_keys(self, key):
        try:
            self.connect.delete(key)
            return to_json_info(errmsg='删除成功')
        except:
            return to_json_info(errno=1, errmsg='删除失败')


class DetectionCookie(object):
    """
    检测模块
    """

    def login(self, username, password):
        params = {
            'username': username,
            'password': password
        }
        requests.get('http://127.0.0.1:9057/spider/login/', params=params)

    @staticmethod
    def _get_cookies(cooks) -> dict:
        """
        获取cookies， 可优化，待修改
        :return:
        """

        cooks = json.loads(cooks.decode('utf-8'))
        cookies = {}
        for coo in cooks:
            name = coo.get('name', 'None')
            value = coo.get('value', 'None')
            cookies[name] = value

        return cookies

    def verify(self, cookies, mobile, password='asdf1123'):

        url = 'https://www.tianyancha.com/usercenter/watch'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
        }
        response = requests.get(url, headers=headers, cookies=cookies)
        html = etree.HTML(response.text)
        user = html.xpath('//div[@nav-type="user"]')
        if not user:
            print(f'mobile_{mobile}---cookie失效-----')
            print(f'mobile{mobile}----重新获取cookie--')
            self.login(mobile, password)
        else:
            print(f'mobile_{mobile}-----cookie正常-----')

    def run(self):
        while True:
            pools = Pools()
            cookie_keys = pools.get_all_keys()
            pool = ThreadPool(8)
            print(f'================{datetime.now()}-开始检测=================')
            for data in cookie_keys:
                request_num = pools.get_one_keys(data, 'request_num')
                # if int(request_num.decode('utf-8')) < 50:
                #     continue

                cookie = pools.get_one_keys(data, 'cookie')
                mobile = pools.get_one_keys(data, 'mobile')
                password = pools.get_one_keys(data, 'password')
                cookies = self._get_cookies(cookie)

                print(f'检测mobile_{mobile}账号--------cookie')
                pool.apply_async(func=self.verify, args=(cookies, mobile, password))

                # gevent.spawn(self.verify, cookies, mobile, password)
            time.sleep(DETECTIONCOOKIE_TIME)


class Scheduling(object):
    """
    调度模块
    """

    def __init__(self):
        self.pools = Pools()
        self.detec = DetectionCookie()

    def __call__(self, *args, **kwargs):
        if START_DETECTIONCOOKIE:
            self.detec.run()


if __name__ == '__main__':
    sche = Scheduling()
    sche()
